import { useRoutes } from "react-router-dom"; //集中式路由
import _ from 'lodash'
import { Spin } from "antd"
import { Suspense } from 'react';
import AuthRoute from './RequireAuth';

import Home from "../Home"; //页面入口文件
import Login from "../Login"; //登录页面
import Errors from "../errors"; //404页面
import Visitorindex from "../home-page/index"; //首页
import VisitorList from "../Visitor-log/VisitorList"; //访客列表
import VisitorAdd from "../Visitor-log/VisitorAdd"; //访客添加修改
import VisitorPrincipal from "../Visitor-log/VisitorPrincipal"; //负责人
import AdminList from "../admin/adminList";
import AdminAdd from "../admin/adminAdd";
import AdminParticulars from "../admin/adminParticulars";
import ExcelList from '../excel/excelList' //审核日志
import SchoolList from '../school/schoolList'//校园动态列表
import SchoolAdd from '../school/SchoolAdd'
import ExcelDeail from '../excel/excelDeail'
import System from "../system/system"; //系统设置
import DepartmentList from "../department/DepartmentList";//部门列表
import DepartmentAddEdit from "../department/DepartmentAddEdit";//部门修改添加
import ReviewerList from "../reviewer/reviewerList";// 核销人员列表  
import ReviewerAddEdit from "../reviewer/reviewerAddEdit";//核销人员添加修改
import ReviewerLog from "../reviewer/reviewerLog";//核销人员日志
import SchoolDetail from '../school/schoolDetail'
import Particulars from "../Visitor-log/list_particulars";

const routeList = [
  {
    path: '/',
    element: <Login />
  },
  {
    path: '/login',
    element: <Login />,
    meta: {
      title: '登录',
    }
  },
  {

    path: '/index', //父路由
    element: <Home />,
    meta: {
      title: '首页',
      requiresAuth: true,
      key: '/index'
    },
    children: [ //子路由
      {
        path: '/index/Visitorindex',
        index: true,
        element: <Visitorindex />,
        meta: {
          requiresAuth: true,
          title: '首页',
          key: '/index/Visitorindex'
        }
      },
      {
        path: '/index/VisitorList',
        element: <VisitorList />,
        meta: {
          requiresAuth: true,
          title: 'VisitorList',
          key: '/index/VisitorList'
        }
      },
      {
        path: '/index/VisitorAdd',
        element: <VisitorAdd />,
        meta: {
          requiresAuth: true,
          title: 'VisitorAdd',
          key: '/index/VisitorAdd'
        }
      },
      {
        path: '/index/Particulars',
        element: <Particulars />,
        meta: {
          requiresAuth: true,
          title: 'Particulars',
          key: '/index/Particulars'
        }
      },
      {
        path: '/index/VisitorPrincipal',
        element: <VisitorPrincipal />,
        meta: {
          requiresAuth: true,
          title: 'VisitorPrincipal',
          key: '/index/VisitorPrincipal'
        }
      },
      {
        path: '/index/ExcelList',
        element: <ExcelList />,
        meta: {
          requiresAuth: true,
          title: 'ExcelList',
          key: '/index/ExcelList'
        }
      },
      {
        path: '/index/SchoolList',
        element: <SchoolList />,
        meta: {
          requiresAuth: true,
          title: 'SchoolList',
          key: '/index/SchoolList'
        }
      },
      {
        path: '/index/SchoolAdd',
        element: <SchoolAdd />,
        meta: {
          requiresAuth: true,
          title: 'SchoolAdd',
          key: '/index/SchoolAdd'
        }
      },
      {
        path: '/index/ExcelDeail',
        element: <ExcelDeail />,
        meta: {
          requiresAuth: true,
          title: 'ExcelDeail',
          key: '/index/ExcelDeail'
        }
      },
      {
        path: '/index/DepartmentList',
        element: <DepartmentList />,
        meta: {
          requiresAuth: true,
          title: 'DepartmentList',
          key: '/index/DepartmentList'
        }
      },
      {
        path: '/index/DepartmentAddEdit',
        element: <DepartmentAddEdit />,
        meta: {
          requiresAuth: true,
          title: 'DepartmentAddEdit',
          key: '/index/DepartmentAddEdit'
        }
      },
      {
        path: '/index/reviewerList',
        element: <ReviewerList />,
        meta: {
          requiresAuth: true,
          title: 'reviewerList',
          key: '/index/reviewerList'
        }
      },
      {
        path: '/index/reviewerAddEdit',
        element: <ReviewerAddEdit />,
        meta: {
          requiresAuth: true,
          title: 'reviewerAddEdit',
          key: '/index/reviewerAddEdit'
        }
      },
      {
        path: '/index/reviewerLog',
        element: <ReviewerLog />,
        meta: {
          requiresAuth: true,
          title: 'reviewerLog',
          key: '/index/reviewerLog'
        }
      },

      {
        path: '/index/SchoolDetail',
        element: <SchoolDetail />,
        meta: {
          requiresAuth: true,
          title: 'SchoolDetail',
          key: '/index/SchoolDetail'
        }
      },
      {
        path: '/index/system',
        element: <System />,
        meta: {
          requiresAuth: true,
          title: 'system',
          key: '/index/system'
        }
      },
      // 管理员列表
      {
        path: "/index/adminList",
        element: <AdminList />,
        meta: {
          requiresAuth: true,
          title: 'adminList',
          key: '/index/adminList'
        }
      },
      // 管理员添加修改
      {
        path: "/index/adminAdd",
        element: <AdminAdd />,
        meta: {
          requiresAuth: true,
          title: 'adminAdd',
          key: '/index/adminAdd'
        }
      },
      // 管理员详情
      {
        path: "/index/adminParticulars",
        element: <AdminParticulars />,
        meta: {
          requiresAuth: true,
          title: 'adminParticulars',
          key: '/index/adminParticulars'
        }
      },
    ]
  },
  {
    path: '/login',
    element: <Login />,
    meta: {
      title: '登录',
      key: 'login',
      // requiresAuth: false,
    }
  },
  {
    path: '*',
    element: <Errors />
  },
]

// 路由拦截
routeList.forEach(item => {
  item.element = <Suspense fallback={<Spin />}>
    <AuthRoute auth={item.meta?.requiresAuth} title={item.meta?.title}>
      {item.element}
    </AuthRoute>
  </Suspense>

  if (item.children && item.children.length > 0) {
    item.children.forEach(sub => {
      sub.element = <Suspense fallback={<Spin />}>
        <AuthRoute auth={sub.meta?.requiresAuth} title={sub.meta?.title}>
          {sub.element}
        </AuthRoute>
      </Suspense>

    })
  }

})


// 默认面包屑
const home = [{
  path: '/index/Visitorindex',
  name: '首页'
}]

const Routesroder: any = () => {
  let route: any = useRoutes(routeList)
  // console.log(route.props.routeContext.outlet?.props.routeContext.matches);
  let matches = route.props.routeContext.outlet?.props.routeContext.matches
  // console.log(matches);
  if (matches) {
    let nMatches = matches.map((item: any) => ({ path: item.route.meta.key, name: item.route.meta.title }))
    window.sessionStorage.setItem('matches', JSON.stringify(_.uniqBy([...home, ...nMatches], 'name')))
  }
  return route
}

export default Routesroder;